package Controller;


import Model.Shebei;
import Service.ShebeiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import utils.ReturnData;
import utils.ReturnDataForLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import utils.permission.PageDataResult;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 分类控制层
 */
@Controller
@RequestMapping("/shebei")
public class ShebeiController{
    private static final Logger logger = LoggerFactory.getLogger(ShebeiController.class);
    @Autowired
    private ShebeiService shebeiService;

    @RequestMapping("/listPage")
    public String listPage(){
        return "SmartCheck/shebei/list";
    }




    @RequestMapping("/getList")
    @ResponseBody
    public ReturnDataForLayui getList(Shebei shebei){
        return shebeiService.getList(shebei);
    }

    @RequestMapping("/getName")
    @ResponseBody
    public ReturnDataForLayui getName(Shebei shebei){
        return shebeiService.getName(shebei);
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return "SmartCheck/shebei/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public ReturnData add(Shebei shebei){
        return shebeiService.add(shebei);
    }

    @RequestMapping("/editPage")
    public String editPage(){
        return "SmartCheck/shebei/edit";
    }

    @RequestMapping("/update")
    @ResponseBody
    public ReturnData update(Shebei shebei){
        return shebeiService.update(shebei);
    }

    @RequestMapping("/updateBeizhu")
    @ResponseBody
    public ReturnData updateBeizhu(Shebei shebei){
        return shebeiService.updateBeizhu(shebei);
    }

    @RequestMapping("/updateState")
    @ResponseBody
    public ReturnData updateState(Shebei shebei){
        return shebeiService.updateState(shebei);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ReturnData delete(Shebei shebei){
        return shebeiService.delete(shebei);
    }





    @RequestMapping(value = "/setShebei", method = RequestMethod.POST,  produces={"text/plain;charset=UTF-8"} )
    @ResponseBody
    public String setShebei( Shebei shebei, HttpSession session) {
        logger.debug("设置设备[新增或更新]！shebei:" + shebei );
        try {
            //两个前端数据的验证
            if (null == shebei) {
                logger.debug("请您填写设备信息");
                return "请您填写设备信息";
            }

            // 设置用户[新增或更新]
            logger.info("设置设备[新增或更新]成功！shebei=" + shebei );
            return shebeiService.setShebei(shebei,  session);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("设置用户[新增或更新]异常！", e);
            return "操作异常，请您稍后再试";
        }
    }

    @RequestMapping(value = "/getShebeis", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult getShebei(
            @RequestParam("page") Integer page,
            @RequestParam("limit") Integer limit,
            Shebei shebeiSearch
    ) {
        PageDataResult pdr = new PageDataResult();
        try {
            if (null == page) {
                page = 1;
            }
            if (null == limit) {
                limit = 10;
            }
            pdr = shebeiService.getShebeis(shebeiSearch, page, limit);
            logger.debug("设备列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("设备列表查询异常！", e);
        }

        return pdr;
    }

    @RequestMapping(value = "/getShebei", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getShebei(@RequestParam("id") Integer id) {
        logger.debug("查询用户数据！userId:" + id);
        Map<String, Object> map = new HashMap<>();
        try {
            if (null == id) {
                logger.debug("请求参数有误，请您稍后再试");
                map.put("msg", "请求参数有误，请您稍后再试");
                return map;
            }
            // 查询用户
            Shebei shebei = shebeiService.getShebei(id);
            logger.debug("查询设备数据！shebei=" + shebei);
            if (null != shebei) {
                map.put("shebei", shebei);
                map.put("msg", "ok");
            } else {
                map.put("msg", "查询信息有误，请您稍后再试");
            }
            logger.debug("查询成功！map=" + map);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "查询设备错误，请您稍后再试");
            logger.error("查询设备数据异常！", e);
        }
        return map;
    }

    @RequestMapping(value = "/delShebei", method = RequestMethod.POST, produces={"text/plain;charset=UTF-8"})
    @ResponseBody
    public String delShebei(
            @RequestParam("id") Integer id,
            HttpSession session
    ) {
        logger.debug("删除设备！id:" + id);
        String msg = "";
        try {
            if (null == id ) {
                logger.debug("删除设备，结果=请求参数有误，请您稍后再试");
                return "请求参数有误，请您稍后再试";
            }
            // 删除用户
            msg = shebeiService.delShebei(id);
            logger.info("删除设备:" + msg + "！userId=" + id );
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除设备异常！", e);
            msg = "操作异常，请您稍后再试";
        }
        return msg;
    }
}